package com.zbkj.admin.config;

import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusPropertiesCustomizer;
import com.mybatisflex.core.datasource.DataSourceDecipher;
import com.mybatisflex.core.logicdelete.LogicDeleteProcessor;
import com.mybatisflex.core.table.DynamicSchemaProcessor;
import com.mybatisflex.core.table.DynamicTableProcessor;
import com.mybatisflex.core.tenant.TenantFactory;
import com.mybatisflex.spring.boot.ConfigurationCustomizer;
import com.mybatisflex.spring.boot.MyBatisFlexCustomizer;
import com.mybatisflex.spring.boot.MybatisFlexProperties;
import com.mybatisflex.spring.boot.SqlSessionFactoryBeanCustomizer;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.scripting.LanguageDriver;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.TypeHandler;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ResourceLoader;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.List;

/**
 * mybatis 配置
 */
@Configuration
@EnableTransactionManagement
@Import({MybatisPlAutoConfiguration.MybatisFlexAutoConfiguration.class, MybatisPlAutoConfiguration.MybatisPlusAutoConfiguration.class})
public class MybatisPlAutoConfiguration {

    @Configuration
    @MapperScan(value = {"com.zbkj.admin.dao"}, sqlSessionFactoryRef = "mybatisPlusSqlSessionFactory", sqlSessionTemplateRef = "mybatisPlusSqlSessionTemplate")
    public static class MybatisPlusAutoConfiguration extends com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration {
        public MybatisPlusAutoConfiguration(MybatisPlusProperties properties, ObjectProvider<Interceptor[]> interceptorsProvider, ObjectProvider<TypeHandler[]> typeHandlersProvider, ObjectProvider<LanguageDriver[]> languageDriversProvider, ResourceLoader resourceLoader, ObjectProvider<DatabaseIdProvider> databaseIdProvider, ObjectProvider<List<com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer>> configurationCustomizersProvider, ObjectProvider<List<com.baomidou.mybatisplus.autoconfigure.SqlSessionFactoryBeanCustomizer>> sqlSessionFactoryBeanCustomizers, ObjectProvider<List<MybatisPlusPropertiesCustomizer>> mybatisPlusPropertiesCustomizerProvider, ApplicationContext applicationContext) {
            super(properties, interceptorsProvider, typeHandlersProvider, languageDriversProvider, resourceLoader, databaseIdProvider, configurationCustomizersProvider, sqlSessionFactoryBeanCustomizers, mybatisPlusPropertiesCustomizerProvider, applicationContext);
        }

        @Bean("mybatisPlusSqlSessionFactory")
        public SqlSessionFactory mybatisPlusSqlSessionFactory(DataSource dataSource) throws Exception {
            return super.sqlSessionFactory(dataSource);
        }

        @Bean("mybatisPlusSqlSessionTemplate")
        public SqlSessionTemplate mybatisPlusSqlSessionTemplate(@Qualifier("mybatisPlusSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
            return super.sqlSessionTemplate(sqlSessionFactory);
        }

    }

    @Configuration
    public static class MybatisFlexAutoConfiguration extends com.mybatisflex.spring.boot.MybatisFlexAutoConfiguration {
        public MybatisFlexAutoConfiguration(MybatisFlexProperties properties, ObjectProvider<Interceptor[]> interceptorsProvider, ObjectProvider<TypeHandler[]> typeHandlersProvider, ObjectProvider<LanguageDriver[]> languageDriversProvider, ResourceLoader resourceLoader, ObjectProvider<DatabaseIdProvider> databaseIdProvider, ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider, ObjectProvider<List<SqlSessionFactoryBeanCustomizer>> sqlSessionFactoryBeanCustomizers, ObjectProvider<DataSourceDecipher> dataSourceDecipherProvider, ObjectProvider<DynamicTableProcessor> dynamicTableProcessorProvider, ObjectProvider<DynamicSchemaProcessor> dynamicSchemaProcessorProvider, ObjectProvider<TenantFactory> tenantFactoryProvider, ObjectProvider<LogicDeleteProcessor> logicDeleteProcessorProvider, ObjectProvider<MyBatisFlexCustomizer> mybatisFlexCustomizerProvider) {
            super(properties, interceptorsProvider, typeHandlersProvider, languageDriversProvider, resourceLoader, databaseIdProvider, configurationCustomizersProvider, sqlSessionFactoryBeanCustomizers, dataSourceDecipherProvider, dynamicTableProcessorProvider, dynamicSchemaProcessorProvider, tenantFactoryProvider, logicDeleteProcessorProvider, mybatisFlexCustomizerProvider);
        }

        @Bean
        @Primary
        public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
            return super.sqlSessionFactory(dataSource);
        }

        @Bean
        @Primary
        public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
            return super.sqlSessionTemplate(sqlSessionFactory);
        }

    }

}
